<template>
  <a-modal
    v-model:visible="visible"
    title="新增楼盘"
    :maskClosable="false"
    @ok="handleOk"
    :style="{
      top: '0px',
      width: winWidth + 'px',
      height: 'auto',
    }"
  >
    <template #footer>
      <a-button key="submit" type="primary" :loading="loading" @click="handleOk"
        >提交</a-button
      >
      <a-button key="back" @click="handleCancel">返回</a-button>
    </template>
  </a-modal>
</template>
<script lang="ts">
import {
  defineComponent,
  ref,
  reactive,
  onMounted,
  onBeforeUnmount,
  defineEmits,
} from "vue";
import ResultEnum from "@/config/ResultEnum";
import { message } from "ant-design-vue";
import { copyObjVal, timestamp } from "@/utils/common";
import DeptSelector from "@/components/deptSelector/index.vue";
import { submit } from "./index";

export default defineComponent({
  components: { DeptSelector },
  setup(props, context) {
    const deptSelector = ref();
    const emit = defineEmits(["refreshTable"]);
    const formRef = ref();
    const visible = ref(false);
    const loading = ref(false);
    const winWidth = ref(document.body.clientWidth);
    const winHeight = ref(document.body.clientHeight - 200);
    const now = ref();

    const formValue = ref({} as any);

    const initData = () => {};

    const display = () => {
      now.value = timestamp();
      visible.value = true;
      initData();
    };

    //确定按钮点击事件
    const handleOk = () => {
      validateParams()
        .then((res: any) => {
          loading.value = true;
          submit(formValue.value)
            .then((res: any) => {
              loading.value = false;
              message.success("提交成功");
              visible.value = false;
              context.emit("refreshTable");
            })
            .catch((res: string) => {
              loading.value = false;
              message.error(res);
            });
        })
        .catch((res: string) => {
          message.error(res);
        });
    };

    //校验数据
    const validateParams = () => {
      return new Promise((resolve: any, reject: any) => {
        resolve("ok");
      });
    };

    const handleCancel = () => {
      //formValue.content = "";
      visible.value = false;
    };

    onMounted(() => {
      window.onresize = () => {
        winWidth.value = document.body.clientWidth / 2;
        winHeight.value = document.body.clientHeight - 200;
      };
    });

    onBeforeUnmount(() => {});

    //返回数据
    return {
      visible,
      display,
      handleOk,
      handleCancel,
      loading,
      formValue,
      winWidth,
      winHeight,
      deptSelector,
      now,
    };
  },
});
</script>
